package com.hhhn.wk.widget.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.main.tab2.view.xiangqing.adapter.ClassifyGridViewAdapter;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.widget.customview.MyGridView;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;

import static java.security.AccessController.getContext;

/**
 * * * * * * * * * * * *
 * @author：WenKe ｡◕‿◕｡ *
 * Data：2017     ｡◕‿◕｡ *
 * QQ：370820580  ｡◕‿◕｡ *
 * Function：     ｡◕‿◕｡ *
 * ViewHolder类
 * * * * * * * * * * *
 */
public class ViewHolder {
    private SparseArray<View> mView;/*存放ListView的item的View*/
    private View convertView;       /*存放item的View*/
    private View item;                /*存放convertView*/
    private int position;           /*游标*/
    //构造方法，完成相关初始化
    private ViewHolder(Context context, int LayoutId, int position) {
        mView = new SparseArray<>();
        this.position = position;
        convertView = LayoutInflater.from(context).inflate(LayoutId, null);
        convertView.setTag(this);
        item = convertView;
    }
    public static ViewHolder bind(Context context, View convertView, ViewGroup parent, int layoutRes, int position) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder(context, layoutRes, position);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.item = convertView;
        }
        holder.position = position;
        return holder;
    }
    public <T extends View> T getView(int viewId) {
        T t = (T) mView.get(viewId);
        if (t == null) {
            t = (T) convertView.findViewById(viewId);
            mView.put(viewId, t);
        }
        return t;
    }
    public <T extends View> T getView(int viewId, Class<? extends View> clazz) {
        T t = (T) mView.get(viewId);
        if (t == null) {
            t = (T) convertView.findViewById(viewId);
            mView.put(viewId, t);
        }
        return t;
    }
    //获取当前条目
    public View getItemView() {
        return item;
    }

    //获取条目位置
    public int getItemPosition() {
        return position;
    }
//
    //设置文字+删除线
    public ViewHolder setFlags(int id, CharSequence text) {
        View view = getView(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
            ((TextView) view).getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线（删除线）
        }
        return this;
    }
    //设置文字
    public ViewHolder setText(int id, CharSequence text) {
        View view = getView(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        }
        return this;
    }
    //设置文字
    public ViewHolder setTextRating(int id, float text) {
        View view = getView(id);
        if (view instanceof RatingBar) {
            ((RatingBar) view).setRating(text);
        }
        return this;
    }

    //设置文字
    public ViewHolder setTextColor(int id, int color) {
        View view = getView(id);
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(color);
        }
        return this;
    }

    //设置图片
    public ViewHolder setImageResource(int id, int drawableRes) {
        View view = getView(id);
        if (view instanceof ImageView) {
            ((ImageView) view).setImageResource(drawableRes);
        } else {
            view.setBackgroundResource(drawableRes);
        }
        return this;
    }

    //设置监听
    public ViewHolder setOnClickListener(int id, View.OnClickListener listener) {
        getView(id).setOnClickListener(listener);
        return this;
    }

    //设置可见
    public ViewHolder setVisibility(int id, int visible) {
        getView(id).setVisibility(visible);
        return this;
    }

    //设置标签
    public ViewHolder setTag(int id, Object obj) {
        getView(id).setTag(obj);
        return this;
    }

    /**
     * 加载网络图片
     * ViewHolder中
     */
    public ViewHolder setImage(int viewid, String url) {
        ImageView view = getView(viewid, ImageView.class);
        ImageOptions options = new ImageOptions.Builder()
//                .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(80))//设置宽高
//                .setRadius(DensityUtil.dip2px(5))//设置圆角
//                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)//图片拉伸
                .setUseMemCache(true)//使用缓存
                .setLoadingDrawableId(R.mipmap.logo)//加载时显示的图片
                .setFailureDrawableId(R.mipmap.logo)//加载失败显示的图片
                .build();
        x.image().bind(view, url, options);
        return this;
    }

    /**
     * 加载网络图片
     * ViewHolder中
     */
    public ViewHolder setImageshanbgiao(int viewid, String url) {
        ImageView view = getView(viewid, ImageView.class);
        ImageOptions options = new ImageOptions.Builder()
//                .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(80))//设置宽高
//                .setRadius(DensityUtil.dip2px(5))//设置圆角
//                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)//图片拉伸
                .setUseMemCache(true)//使用缓存
                .setLoadingDrawableId(R.mipmap.logo)//加载时显示的图片
                .setFailureDrawableId(R.mipmap.logo)//加载失败显示的图片
                .build();
        x.image().bind(view, url, options);
        return this;
    }

    /**
     * 加载网络图片--圆角
     * ViewHolder中 正常
     */
    public ViewHolder setImageyuan(int viewid, String url) {
        ImageView view = getView(viewid, ImageView.class);
        ImageOptions options = new ImageOptions.Builder()
                .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(180))//设置宽高

                .setRadius(DensityUtil.dip2px(10))//设置圆角
//                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)//图片拉伸
                .setUseMemCache(true)//使用缓存
                .setLoadingDrawableId(R.mipmap.logo)//加载时显示的图片
                .setFailureDrawableId(R.mipmap.logo)//加载失败显示的图片
                .build();
        x.image().bind(view, Constants.HTTPIMG+ url, options);
        Log.d("加载网络图片", "setImageyuan: "+Constants.HTTPIMG+ url);
        return this;
    }
    /**
     * 加载网络图片--圆角
     * ViewHolder中 正常
     */
    public ViewHolder setImagechang(int viewid, String url)
    {
        ImageView view = getView(viewid, ImageView.class);
        ImageOptions options = new ImageOptions.Builder()
//                .setSize(LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(180))//设置宽高
                .setRadius(DensityUtil.dip2px(10))//设置圆角
                .setImageScaleType(ImageView.ScaleType.FIT_XY)//图片拉伸
                .setUseMemCache(true)//使用缓存
                .setLoadingDrawableId(R.mipmap.logo)//加载时显示的图片
                .setFailureDrawableId(R.mipmap.logo)//加载失败显示的图片
                .build();
        x.image().bind(view, Constants.HTTPIMG+ url, options);
        Log.d("@@", "setImagechang: "+ Constants.HTTPIMG+ url);
        return this;
    }





}
